import { mergeLocalCart, findCartList, insertCart, deleteCart, updateCart, checkAllCart } from '@/api/cart'
// 购物车状态
export default {
  namespaced: true,
  state: () => ({
    list: []
  }),
  getters: {
    // 有效商品列表,购物车页面列表渲染
    validList: state => {
      return state.list.filter(item => item.isEffective)
    },
    // 选中的购物车商品数据
    validSeled: (state, getters) => {
      return getters.validList.filter(item => item.selected)
    },
    validSeledTotal: (state, getters) => {
      return getters.validSeled.reduce((acc, item) => (acc += item.count * item.nowPrice), 0)
    },
    // 有效商品是否是全部选中状态
    isAll: (state, getterts) => {
      return getterts.validList.every(item => item.selected)
    }
  },
  mutations: {
    // 设置购物车数据
    setList (state, list) {
      state.list = list
    },
    // 加入购物车
    setGoodList (state, good) {
      const goodIndex = state.list.findIndex(item => item.skuId === good.skuId)
      if (goodIndex > -1) {
        state.list[goodIndex].count += good.count
      } else {
        state.list.unshift(good)
      }
    },
    // 单选功能
    singelCheck (state, { good, isCheck }) {
      const checkState = state.list.find(item => item.skuId === good.skuId)
      checkState.selected = isCheck
    },
    // 多选功能
    allCheck (state, allCheck) {
      state.list.forEach(item => {
        item.selected = allCheck
      })
    },
    // 删除功能
    delCart (state, good) {
      const delIndex = state.list.findIndex(item => item.skuId === good.skuId)
      state.list.splice(delIndex, 1)
    },
    // 数量变化
    changeNum (state, { good, count }) {
      const ListNum = state.list.find(item => item.skuId === good.skuId)
      ListNum.count = count
    }
  },
  actions: {
    // 获取购物车
    async getListAction ({ commit }) {
      const result = await findCartList()
      commit('setList', result)
    },
    // 合并购物车
    async mergeCartAction ({ state, dispatch }) {
      if (state.list.length > 0) {
        const mergeData = state.list.map(item => {
          return {
            skuId: item.skuId,
            selected: item.selected,
            count: item.count
          }
        })
        console.log(mergeData)
        await mergeLocalCart(mergeData)
      }
      dispatch('getListAction')
    },
    // 加入购物车
    async setGoodListAction ({ commit, rootState, dispatch }, good) {
      // commit('updateValue', good);
      if (rootState.user.profile.token) {
        console.log('已登录')
        await insertCart({ skuId: good.skuId, count: good.count })
        dispatch('getListAction')
        return '添加成功'
      } else {
        console.log('未登录')
        commit('setGoodList', good)
        return '添加成功'
      }
    },
    // 单选功能
    async singelCheckAction ({ commit, rootState, dispatch }, { good, isCheck }) {
      // commit('updateValue', good);
      if (rootState.user.profile.token) {
        await updateCart({ ...good, selected: isCheck })
        dispatch('getListAction')
      } else {
        commit('singelCheck', { good, isCheck })
      }
    },
    // 多选功能
    async allCheckAction ({ commit, rootState, getters, dispatch }, allCheck) {
      // commit('updateValue', good);
      if (rootState.user.profile.token) {
        const ids = getters.validList.map(item => item.skuId)
        await checkAllCart({ selected: allCheck, ids })
        dispatch('getListAction')
      } else {
        commit('allCheck', allCheck)
      }
    },
    // 删除功能
    async delCartAction ({ commit, rootState, dispatch }, good) {
      // commit('updateValue', good);
      if (rootState.user.profile.token) {
        await deleteCart([good.skuId])
        dispatch('getListAction')
      } else {
        commit('delCart', good)
      }
    },
    // 数量变化
    async changeNumAction ({ commit, rootState, dispatch }, { good, count }) {
      // commit('updateValue', good);
      if (rootState.user.profile.token) {
        await updateCart({ ...good, count })
        dispatch('getListAction')
      } else {
        commit('changeNum', { good, count })
      }
    }
  }
}
